﻿using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;
using Tfs.OnTheRoad.Model;
namespace Tfs.OnTheRoad.ViewModel
{
    /// <summary>
    /// Work Items Overview ViewModel
    /// </summary>
    public class WorkItemsOverviewViewModel : BaseViewModel
    {
        public override void Refresh()
        {
            //We just refresh what was already loaded
            foreach (var item in WorkItemTypes)
                if (item.Loaded)
                    item.LoadData();
        }

        private WorkItemsChartViewModel _workItemsChartViewModel;

        public WorkItemsChartViewModel WorkItemsChartViewModel
        {
            get { return _workItemsChartViewModel; }
            set { _workItemsChartViewModel = value; }
        }

        public WorkItemsOverviewViewModel(string[] workItemTypes, string[] states)
            : this(null, workItemTypes, states)
        {
        }
        public WorkItemsOverviewViewModel(Query currentQuery, string[] types)
            : this(currentQuery, types, null)
        {
        }
        public WorkItemsOverviewViewModel(Query currentQuery, string[] types, string[] states)
            : base()
        {
            _workItemsChartViewModel = new ViewModel.WorkItemsChartViewModel(currentQuery, types, states);
            CurrentQuery = currentQuery;
            System.Text.StringBuilder additionalQueryParameters = new System.Text.StringBuilder();
            var currentProject = PhoneApplicationService.Current.State["CurrentProject"] as Project;
            if (currentQuery != null)
            {
                PageTitle = string.Format("{0} \\ {1}", currentProject.Name, CurrentQuery.Name);
            }
            else
            {
                PageTitle = string.Format("{0} \\ {1} \\ {2}", currentProject.Name, string.Join(",", types), string.Join(",", states));
                if (states != null && states.Length > 0)
                {
                    foreach (string state in states)
                        additionalQueryParameters.AppendFormat("State eq '{0}' or ", state);
                    additionalQueryParameters.Remove(additionalQueryParameters.Length - 4, 4);//4 = ' Or '
                }
            }
            if (types != null)
            {
                _workItemTypes = new ObservableCollection<WorkItemListViewModel>();
                foreach (string type in types)
                {
                    WorkItemListViewModel itemViewModel = new WorkItemListViewModel()
                    {
                        WorkItemType = type,
                        Query = string.Format("Type eq '{0}'", type)
                    };
                    if (currentQuery != null)
                    {
                        itemViewModel.WorkItemQuery = currentQuery;
                        itemViewModel.EntityCollectionName = string.Format("Queries('{0}')/WorkItems", CurrentQuery.Id);
                    }
                    else if (states != null && states.Length > 0)
                    {
                        //Then we need to set the proper EntityCollection!
                        itemViewModel.EntityCollectionName = string.Format("Projects('{0}')/WorkItems", currentProject.Name);
                        itemViewModel.Query += string.Format(" and ({0})", additionalQueryParameters.ToString());
                    }
                    _workItemTypes.Add(itemViewModel);
                }
            }
            else
            {//No types yet(possibly the guy came too fast(project summary was not ready yet...))? just bring all
                _workItemTypes = new ObservableCollection<WorkItemListViewModel>();
                WorkItemListViewModel itemViewModel = new WorkItemListViewModel();
                if (currentQuery != null)
                {
                    itemViewModel.WorkItemQuery = currentQuery;
                    itemViewModel.EntityCollectionName = string.Format("Queries('{0}')/WorkItems", CurrentQuery.Id);
                }
                else if (states != null && states.Length > 0)
                {
                    //Then we need to set the proper EntityCollection!
                    itemViewModel.EntityCollectionName = string.Format("Projects('{0}')/WorkItems", currentProject.Name);
                    itemViewModel.Query += string.Format("({0})", additionalQueryParameters.ToString());
                }
                _workItemTypes.Add(itemViewModel);
            }
        }

        private bool _processing;

        public bool Processing
        {
            get { return _processing; }
            set
            {
                _processing = value;
                this.RaisePropertyChanged("Processing");
            }
        }

        bool summaryLoaded = false;
        public void CalculateSummary()
        {
            if (!summaryLoaded)
            {
                _workItemsChartViewModel.CalculateSummary();
                summaryLoaded = true;
            }
        }

        private Query _currentQuery;

        public Query CurrentQuery
        {
            get { return _currentQuery; }
            set { _currentQuery = value; }
        }
        
        private ObservableCollection<WorkItemListViewModel> _workItemTypes;

        public ObservableCollection<WorkItemListViewModel> WorkItemTypes
        {
            get { return _workItemTypes; }
            set
            {
                _workItemTypes = value;
            }
        }             
    }
}
